package com.iscreen.dbupgrade.queries.mysql;

import java.util.ArrayList;
import java.util.List;

import com.iscreen.dbupgrade.objects.InsertRows;
import com.iscreen.dbupgrade.queries.InsertValuesQuery;
import com.iscreen.dbupgrade.queries.constants.DBConstants;
import com.iscreen.utils.strings.StringUtils;

public class MySqlInsertValues extends InsertValuesQuery
{

	protected MySqlInsertValues(InsertRows query)
	{
		super(query);
	}

	@Override
	public String getSql()
	{
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(DBConstants.INSERT_INTO + " " + getTableName() + " ");
		
		List<String> fieldNamesList = new ArrayList<String>();
		List<String> fieldValuesList = new ArrayList<String>();
		
		for (String fieldName : getFieldNames())
		{
			fieldNamesList.add(fieldName);
			fieldValuesList.add("?");
		}
		
		sqlBuilder.append(" (");
		sqlBuilder.append(StringUtils.listToString(fieldNamesList, ","));
		sqlBuilder.append(") ");

		sqlBuilder.append(DBConstants.VALUES);
		sqlBuilder.append(" (");
		sqlBuilder.append(StringUtils.listToString(fieldValuesList, ","));
		sqlBuilder.append(") ");

		return sqlBuilder.toString();
	}

}
